package jaspect.model;

import jaspect.Main;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Locale;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

/**
 * @author 
 *
 */
public class Compiler {

    private Main mi;

    /**
     *
     * @param sourceFile
     * @param outputFolder
     * @param serviceClassPath
     * @return
     * @throws Exception
     */
    public String generateByteCode(String sourceFile, String outputFolder, String serviceClassPath) throws Exception {

        String[] cargs = new String[]{sourceFile, outputFolder};

        compile(cargs);


        return null;



    }
/**
 * This method calls the java compiler to compile the translated ajava files
 *
 * @param args
 * @throws Exception
 */
    public void compile(String[] args) throws Exception {
        mi.setCompileComments("");
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        MyDiagnosticListener listener = new MyDiagnosticListener();
        listener.setMi(getMi());
        StandardJavaFileManager fileManager =
                compiler.getStandardFileManager(listener, null, null);
        String fileToCompile = args[0];


        Iterable<? extends JavaFileObject> fileObjects = fileManager.getJavaFileObjectsFromStrings(
                Arrays.asList(fileToCompile));
        ArrayList<String> options = new ArrayList<String>();
		options.add("-d");
		options.add(args[0].substring(0,args[0].lastIndexOf("\\")));
		




        CompilationTask task = compiler.getTask(null, fileManager, listener, options, null, fileObjects);

        Boolean result = task.call();
        if (result == true) {
            mi.setCompileComments("Compilation has succeeded");

        } else {
            mi.setCompileComments("Compilation failed");

        }
    }

    /**
     * @return the mi
     */
    public Main getMi() {
        return mi;
    }

    /**
     * @param mi the mi to set
     */
    public void setMi(Main mi) {
        this.mi = mi;
    }
}

class MyDiagnosticListener implements DiagnosticListener<JavaFileObject> {

    private Main mi;

    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
        StringBuilder sb = new StringBuilder();
        sb.append("Code->" + diagnostic.getCode());
        sb = Utils.addNewline(sb);
        sb.append("Column Number->" + diagnostic.getColumnNumber());
        sb = Utils.addNewline(sb);
        sb.append("End Position->" + diagnostic.getEndPosition());
        sb = Utils.addNewline(sb);
        sb.append("Kind->" + diagnostic.getKind());
        sb = Utils.addNewline(sb);
        sb.append("Line Number->" + diagnostic.getLineNumber());
        sb = Utils.addNewline(sb);
        sb.append("Message->" + diagnostic.getMessage(Locale.ENGLISH));
        sb = Utils.addNewline(sb);
        sb.append("Position->" + diagnostic.getPosition());
        sb = Utils.addNewline(sb);
        sb.append("Source" + diagnostic.getSource());
        sb = Utils.addNewline(sb);
        sb.append("Start Position->" + diagnostic.getStartPosition());
        sb = Utils.addNewline(sb);
        mi.setCompileComments(sb.toString());
    }

    /**
     * @return the mi
     */
    public Main getMi() {
        return mi;
    }

    /**
     * @param mi the mi to set
     */
    public void setMi(Main mi) {
        this.mi = mi;
    }
}



